import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.BoxLayout;


public class resultsScreen extends JFrame {

	private JPanel contentPane;
	private boggleGame boggleResults;

	public resultsScreen(boggleGame currentBoggleGame) {
		setTitle("Results");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 870, 608);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		setLocationRelativeTo(null);

		this.boggleResults = currentBoggleGame;
		boggleResults.generateResults();

		//Board Stats
		{
			//Label
			JLabel lblBoardStats = new JLabel("Board Stats:");
			lblBoardStats.setFont(new Font("Tahoma", Font.BOLD, 16));
			lblBoardStats.setBounds(10, 15, 203, 14);
			contentPane.add(lblBoardStats);

			//Text Area
			JTextArea boardStats = new JTextArea();
			boardStats.setFont(new Font("Monospaced", Font.BOLD, 13));
			boardStats.setEditable(false);
			boardStats.setBounds(10, 36, 203, 200);
			contentPane.add(boardStats);

			//Get Stats
			int[] wordStats = boggleResults.getWordStats();
			StringBuilder boggleStats = new StringBuilder();
			boggleStats.append("Available Words:\n");
			boggleStats.append(String.format("3-4 Letters: %d (1 pt)\n", wordStats[0]));
			boggleStats.append(String.format("5 Letters: %d (2 pts)\n", wordStats[1]));
			boggleStats.append(String.format("6 Letters: %d (3 pts)\n", wordStats[2]));
			boggleStats.append(String.format("7 Letters: %d (5 pts)\n", wordStats[3]));
			boggleStats.append(String.format("8 Letters: %d (11 pts)\n", wordStats[4]));
			boggleStats.append(String.format("9 Letters: %d (2 pts/ltr)\n", wordStats[5]));
			boggleStats.append(String.format("Total Words: %d", boggleResults.getExistingWords().size()));
			boardStats.setText(boggleStats.toString());
		}

		//Common Words
		{
			//Label
			JLabel lblCommonWords = new JLabel("Common Words:");
			lblCommonWords.setFont(new Font("Tahoma", Font.BOLD, 16));
			lblCommonWords.setBounds(225, 15, 201, 14);
			contentPane.add(lblCommonWords);

			//Text Area
			JTextArea commonWordsArea = new JTextArea();
			commonWordsArea.setFont(new Font("Monospaced", Font.BOLD, 13));
			commonWordsArea.setText("");
			commonWordsArea.setEditable(false);
			commonWordsArea.setBounds(223, 36, 203, 200);
			contentPane.add(commonWordsArea);

			//Scroll Bar
			JScrollPane commonScroll = new JScrollPane(commonWordsArea);
			commonScroll.setBounds(commonWordsArea.getBounds());
			getContentPane().add(commonScroll);

			//Get Common Words
			StringBuilder commonWordsBuilder = new StringBuilder();
			for(String word : boggleResults.getCommonWords()){
				commonWordsBuilder.append(word + "\n");
			}
			commonWordsArea.setText(commonWordsBuilder.toString());
		}

		//Player Stats
		{
			//Label
			JLabel lblPlayerStats = new JLabel("Player Stats:");
			lblPlayerStats.setFont(new Font("Tahoma", Font.BOLD, 16));
			lblPlayerStats.setBounds(434, 15, 203, 14);
			contentPane.add(lblPlayerStats);

			//Scroll Bar
			JScrollPane playerStatsScroll = new JScrollPane();
			playerStatsScroll.setBounds(436, 36, 418, 500);
			contentPane.add(playerStatsScroll);

			//JPanel
			JPanel playerStatsPanel = new JPanel();
			playerStatsScroll.setViewportView(playerStatsPanel);
			playerStatsPanel.setLayout(new GridLayout(0, 1, 0, 0));

			//Add players to panel
			for(player play : boggleResults.getPlayers()){
				playerStats newPlayerStats = new playerStats(play, boggleResults.getCommonWords());
				newPlayerStats.setPreferredSize(new Dimension(380, 400));
				playerStatsPanel.add(newPlayerStats);
			}
		}

		//Word List
		{
			//Label
			JLabel lblWordList = new JLabel("Total Word List:");
			lblWordList.setFont(new Font("Tahoma", Font.BOLD, 16));
			lblWordList.setBounds(10, 247, 203, 14);
			contentPane.add(lblWordList);

			//Scroll Bar
			JScrollPane wordListScroll = new JScrollPane();
			wordListScroll.setBounds(10, 272, 416, 297);
			contentPane.add(wordListScroll);

			//JPanel
			JPanel wordListPanel = new JPanel();
			wordListScroll.setViewportView(wordListPanel);
			wordListPanel.setLayout(new GridLayout(0, 4, 0, 0));

			//Get Word List
			for(String word : boggleResults.getExistingWords()){
				JLabel newWord = new JLabel(word);
				if(boggleResults.wordGuessed(word)){
					newWord.setForeground(new Color(60, 179, 113));
				}
				wordListPanel.add(newWord);
			}
		}

		//Back to Main Menu
		JButton btnNewButton = new JButton("Back to Main Menu");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(JOptionPane.showConfirmDialog(null, "Return to Main Menu?", "Return to Main Menu?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
					mainMenu menu = new mainMenu();
					menu.setVisible(true);
					dispose();
				}
			}
		});
		btnNewButton.setBounds(436, 547, 416, 23);
		contentPane.add(btnNewButton);
	}
}
